package com.chhh.cranelease.mapper;

import com.chhh.cranelease.entity.CarLog;
import com.chhh.cranelease.entity.PeopleLog;
import com.chhh.cranelease.entity.Repair;
import com.chhh.cranelease.entity.Staff;
import com.chhh.cranelease.model.PeopleLogModel;
import com.chhh.cranelease.model.RepairModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface IntegratedmanageMapper {


    @Select("<script>" +
            "select pl_id, pl_name,pl_card ,pl_workdays,pl_license,pl_diverdays,state_name from people_log left join staffstate on state_id=pl_state" +
            "<where>" +
            "<if test='s_name!=null and s_name!=\"\"'>and pl_name like CONCAT('%',#{s_name},'%')</if>" +
            "<if test='s_card!=null and s_card!=\"\"'>and pl_card like CONCAT('%',#{s_card},'%')</if>" +
            "<if test='pl_license!=null and pl_license!=\"\"'>and pl_license like CONCAT('%',#{pl_license},'%')</if>" +
            "<if test='state_name!=null and state_name!=\"\"'>and state_name like CONCAT('%',#{state_name},'%')</if>" +
            "</where>" +
            "</script>")
    List<PeopleLogModel> selectAllPeopleLog(@Param("s_card") String s_card,@Param("s_name") String s_name,@Param("state_name") String state_name,@Param("pl_license") String pl_license);


    @Update("update people_log set pl_workdays=#{pl_workdays},pl_license=#{pl_license},pl_diverdays=#{pl_diverdays},pl_state=#{pl_state} where pl_id = #{pl_id}")
    Integer updatePeopleLog(PeopleLog peopleLog);

    // 当新招人以后插入人员记录
    @Insert("insert into people_log (pl_card,pl_name,pl_workdays,pl_diverdays,pl_license,pl_state)" +
            "values(#{s_card},#{s_name},0,0,null,#{s_state})")
    void insertPeopleLog(Staff staff);

    // 当出行记录添加或者修改为维修是插入来作为维修的数据
    @Insert("insert into repair (r_num,r_state,r_cost,r_date,r_return)values (#{cl_num},#{cl_state},0,#{cl_outtime},null)")
    void insertRepair(CarLog carLog);

    @Select("<script>" +
            "select r_id, r_num,clstate_name,r_cost,r_date,r_return from repair left join cl_state on clstate_id=r_state" +
            "<where>" +
            "<if test='r_highcost!=null and r_lowcost!=null'>and (r_cost &gt;= #{r_lowcost}) and (r_cost &lt;= #{r_highcost}) </if>" +
            "<if test='r_lowcost==null and r_highcost!=null'>and  r_cost &lt;= #{r_highcost}</if>" +
            "<if test='r_highcost==null and r_lowcost!=null'>and  r_cost &gt;= #{r_lowcost}</if>" +
            "<if test='r_num!=null and r_num!=\"\"'>and r_num like CONCAT('%',#{r_num},'%')</if>" +
            "</where>" +
            "</script>")
    List<RepairModel> selectAllRepair(@Param("r_num") String r_num, @Param("r_highcost") Double r_highcost, @Param("r_lowcost") Double r_lowcost);

    @Update("update repair set r_state=#{r_state},r_cost=#{r_cost},r_date=#{r_date},r_return=#{r_return} where r_id = #{r_id}")
    Integer updateRepair(Repair repair);

    // 当员工离职后
    @Update("update people_log set pl_state=#{s_state} where pl_name=#{s_name}")
    void updatePeopleLeave(Staff staff);
}